Home:ALL Converter>"Resolve" functions in JavaScript promises

"Resolve" functions in JavaScript promises

Ask Time:2015-06-01T23:11:27         Author:Jessica

Json Formatter

I am reading this documentation about how to use promises, and frequently "resolve" and "reject" are passed in as arguments to the Promise constructor, even though nobody ever defined the "resolve" or "reject" functions. How is that possible? Don't we have to define functions before using them?

Here's an example: (source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Browser_compatibility)

var p1 = new Promise(
        // The resolver function is called with the ability to resolve or
        // reject the promise
        function(resolve, reject) {
            log.insertAdjacentHTML('beforeend', thisPromiseCount +
                ') Promise started (<small>Async code started</small>)<br/>');
            // This only is an example to create asynchronism
            window.setTimeout(
                function() {
                    // We fulfill the promise !
                    resolve(thisPromiseCount);
                }, Math.random() * 2000 + 1000);
        });

Author:Jessica,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30577046/resolve-functions-in-javascript-promises
yy